package com.yex.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yex.base.WalletOper;
import com.yex.base.WalletOperEnum;
import com.yex.base.ZeroResultException;
import com.yex.common.utils.PageUtils;
import com.yex.dao.InvestDao;
import com.yex.entity.*;
import com.yex.service.*;
import com.yex.utils.JsonResultVo;
import com.yex.utils.MathUtil;
import com.yex.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;


@Service("investService")
public class InvestServiceImpl extends ServiceImpl<InvestDao, InvestEntity> implements InvestService {
    @Autowired
    private InvestLogService investLogService;
    @Autowired
    private LocaleMessageSourceService message;
    @Autowired
    private UserService userService;
    @Autowired
    private WalletService walletService;
    @Autowired
    private SysConfigService sysConfigService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<InvestEntity> page = this.selectPage(
                new Query<InvestEntity>(params).getPage(),
                new EntityWrapper<InvestEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public JsonResultVo updateBuyInvest(String number, Integer userId) {

        WalletEntity wallet = walletService.selectByUserId(userId);
        if (wallet == null) {
            return new JsonResultVo(message.getMessage("parameter_error"), -1);
        }

        WalletOper walletOper = new WalletOper();
        walletOper.setUserId(userId);
        BigDecimal investRate = MathUtil.getBigDecimal(MathUtil.div(new BigDecimal(sysConfigService.getValue("investRate")), new BigDecimal("100")), 2);
        BigDecimal amount = MathUtil.getBigDecimal(MathUtil.div(new BigDecimal(number), investRate), 2);
        walletOper.setAmount(amount);
        walletOper.setRemark("购买排单币");
        walletOper.setOper(WalletOperEnum.REDUCE);
        walletOper.setId(wallet.getId());
        walletService.updateWallet(walletOper);

        InvestEntity investEntity = this.selectByUserId(userId);
        WalletOper investOper = new WalletOper();
        investOper.setUserId(userId);
        investOper.setAmount(new BigDecimal(number));
        investOper.setRemark("购买排单币");
        investOper.setOper(WalletOperEnum.ADD);
        if (investEntity != null) {
            investOper.setId(investEntity.getId());
        }
        this.updateInvest(investOper);

        return new JsonResultVo(message.getMessage("success"));
    }

    @Override
    @Transactional
    public JsonResultVo updateTransferInvest(String mobile, String number, Integer userId) {

        UserEntity userEntity = userService.selectUserByMobile(mobile);
        if (userEntity == null) {
            return new JsonResultVo(message.getMessage("parameter_error"), -1);
        }

        InvestEntity walletTurnOut = baseMapper.selectByUserId(userId);
        if (walletTurnOut == null) {
            return new JsonResultVo(message.getMessage("parameter_error"), -1);
        }
        WalletOper walletOperTurnOut = new WalletOper();
        walletOperTurnOut.setUserId(userId);
        walletOperTurnOut.setAmount(new BigDecimal(number));
        walletOperTurnOut.setRemark("转出排单币");
        walletOperTurnOut.setOper(WalletOperEnum.REDUCE);
        walletOperTurnOut.setId(walletTurnOut.getId());
        walletOperTurnOut.setTurnUserId(userEntity.getId());
        this.updateInvest(walletOperTurnOut);

        InvestEntity walletTurnInto = baseMapper.selectByUserId(userEntity.getId());
        WalletOper walletOperTurnInto = new WalletOper();
        walletOperTurnInto.setUserId(userEntity.getId());
        walletOperTurnInto.setAmount(new BigDecimal(number));
        walletOperTurnInto.setRemark("转入排单币");
        walletOperTurnInto.setOper(WalletOperEnum.ADD);
        if (walletTurnInto != null) {
            walletOperTurnInto.setId(walletTurnInto.getId());
        }
        walletOperTurnInto.setTurnUserId(userId);
        this.updateInvest(walletOperTurnInto);

        return new JsonResultVo(message.getMessage("success"));
    }

    @Override
    public InvestEntity selectByUserId(Integer userId) {
        return baseMapper.selectByUserId(userId);
    }

    @Override
    @Transactional
    public JsonResultVo updateInvest(WalletOper walletOper) {
        InvestEntity investEntity = new InvestEntity();
        investEntity.setUserId(walletOper.getUserId());
        if (walletOper.getId() == null && WalletOperEnum.ADD == walletOper.getOper()) {
            investEntity.setTotal(walletOper.getAmount());
            int result = baseMapper.insertWallet(investEntity);
            if (result <= 0) {
                throw new ZeroResultException(new JsonResultVo(message.getMessage("error_info_zero")));
            }
            InvestEntity invest = baseMapper.selectByUserId(walletOper.getUserId());
            InvestLogEntity investLogEntity = new InvestLogEntity();
            investLogEntity.setUserId(walletOper.getUserId());
            investLogEntity.setStatus(walletOper.getOper().ordinal());
            investLogEntity.setRemark(walletOper.getRemark());
            investLogEntity.setTotal(walletOper.getAmount());
            investLogEntity.setWalletId(invest.getId());
            investLogEntity.setCreateTime(new Date());
            if (!investLogService.insert(investLogEntity)) {
                throw new ZeroResultException(new JsonResultVo(message.getMessage("error_info_zero")));
            }

            return result > 0 ? new JsonResultVo(message.getMessage("success"), 0) : new JsonResultVo(message.getMessage("fail"), -1);
        }

        investEntity.setId(walletOper.getId());

        if (walletOper.getAmount().compareTo(BigDecimal.ZERO) < 0) {
            throw new ZeroResultException(new JsonResultVo(message.getMessage("error_info_zero")));
        }

        int result = -1;
        if (WalletOperEnum.FROZEN == walletOper.getOper()) {
            investEntity.setFrozen(walletOper.getAmount());
            result = baseMapper.updateFrozen(investEntity);
        } else if (WalletOperEnum.UNFRONZEN == walletOper.getOper()) {
            investEntity.setFrozen(walletOper.getAmount());
            result = baseMapper.updateUnFrozen(investEntity);
        } else if (WalletOperEnum.ADD == walletOper.getOper()) {
            investEntity.setTotal(walletOper.getAmount());
            result = baseMapper.updateAdd(investEntity);
        } else if (WalletOperEnum.REDUCE == walletOper.getOper()) {
            investEntity.setTotal(walletOper.getAmount());
            result = baseMapper.updateReduce(investEntity);
        } else if (WalletOperEnum.REDUCE_FROZEN == walletOper.getOper()) {
            investEntity.setFrozen(walletOper.getAmount());
            result = baseMapper.updateReduceFrozen(investEntity);
        }

        if (result <= 0) {
            throw new ZeroResultException(new JsonResultVo(message.getMessage("error_info_zero")));
        }

        InvestLogEntity investLogEntity = new InvestLogEntity();
        investLogEntity.setUserId(walletOper.getUserId());
        if (walletOper.getTurnUserId() != null) {
            investLogEntity.setTurnUserId(walletOper.getTurnUserId());
        } else {
            investLogEntity.setTurnUserId(0);
        }
        investLogEntity.setStatus(walletOper.getOper().ordinal());
        investLogEntity.setRemark(walletOper.getRemark());
        investLogEntity.setTotal(walletOper.getAmount());
        investLogEntity.setWalletId(walletOper.getId());
        investLogEntity.setCreateTime(new Date());
        if (!investLogService.insert(investLogEntity)) {
            throw new ZeroResultException(new JsonResultVo(message.getMessage("error_info_zero")));
        }

        return new JsonResultVo(message.getMessage("success"));
    }


}
